﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Awaken.Utils.WidgetCode
{
    /// <summary>
    /// 浏览器信息
    /// </summary>
    public static class BrowserInfo
    {
        public static volatile string[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "blackberry", "nokia", "samsung", "meizu","motorola", "wap-", "wapa", "wapi", "wapp", "wapr", };

        /// <summary>
        /// 判断是否是手机访问
        /// </summary>
        /// <returns></returns>
        public static bool IsMobile(string userAgent)
        {
            if (!string.IsNullOrEmpty(userAgent))
            {
                userAgent = userAgent.ToLower();

                if (mobileAgents.Any(t => userAgent.IndexOf(t) >= 0))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断手机浏览器访问者来源
        /// </summary>
        /// <returns></returns>
        public static FeedSource.Source SourcePhoneWeb(string userAgent)
        {
            // 默认端
            if (string.IsNullOrEmpty(userAgent)) return FeedSource.Source.Web;

            userAgent = userAgent.ToLower();

            // 微信
            if (userAgent.Contains("micromessenger"))
                return FeedSource.Source.Weixin;

            //苹果
            if (userAgent.Contains("iphone"))
                return FeedSource.Source.Ios;

            //安卓
            if (userAgent.Contains("android"))
                return FeedSource.Source.Android;

            return FeedSource.Source.Web;
        }
    }
}
